/*
package study.HL7;

import cn.hutool.db.Session;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.*;

@Controller
@ServerEndpoint(value = "/websocket/hl7message")
@Slf4j
public class HL7WebSocket {

    private Gson mGson = new GsonBuilder().serializeNulls().create();

    private static Set<Session> sessions = new HashSet<>();

    @OnOpen
    public void open(Session session) {
        HL7WebSocket.sessions.add(session);
        log.info("有新连接加入");
    }

    @OnMessage
    public void receive(Session session, String msg) {
        try {
            log.info("收到新的消息请求：" + msg);
            String[] split = msg.split(",");
            //遍历当前推送的车辆信息，将缺失的车辆补充进来
            for (String tCardCode : split) {
                boolean tFlag = false;
                for (JianHuYiListView tJianHuYiListView : HL7Business.mJianHuYiListView) {
                    if (tCardCode.equals(tJianHuYiListView.getCar_code())) {
                        tFlag = true;
                        break;
                    }
                }
                if (!tFlag) {
                    JianHuYiListView ttJianHuYiListView = new JianHuYiListView();
                    ttJianHuYiListView.init(tCardCode);
                    HL7Business.mJianHuYiListView.add(ttJianHuYiListView);
                }
            }
            //遍历当前推送信息，将多余的车辆移除
            Iterator<JianHuYiListView> iterator = HL7Business.mJianHuYiListView.iterator();
            while(iterator.hasNext()){
                JianHuYiListView tJianHuYiListView = iterator.next();
                boolean tFlag = false;
                for (String tCardCode : split) {
                    if (tCardCode.equals(tJianHuYiListView.getCar_code())) {
                        tFlag = true;
                        break;
                    }
                }
                if (!tFlag) {
                    iterator.remove();
                }
            }
            this.sortCarcode(HL7Business.mJianHuYiListView, Arrays.asList(split));
            this.broadcast(session, mGson.toJson(HL7Business.mJianHuYiListView));
        } catch (Exception e) {
            e.printStackTrace();
            log.info("receive异常：" + e.getMessage());
        }
    }

    public void broadcast(Session session, String msg) {
        try {
            if (session != null) {
                session.getBasicRemote().sendText(msg);
            }
        } catch (Exception e) {
            log.error("websocket推送失败");
            e.printStackTrace();
        }
    }

    public static void broadcast(String msg) {
        log.info("推送HL7转义消息：" + msg);
        for (Session ses : HL7WebSocket.sessions) {
            try {
                ses.getBasicRemote().sendText(msg);
            } catch (Exception e) {
                log.error("websocket推送失败");
                e.printStackTrace();
                HL7WebSocket.sessions.remove(ses);
            }
        }
    }

    */
/**
     * 发生异常错误
     *
     * @param session
     * @param error
     * @throws IOException
     *//*

    @OnError
    public void onError(Session session, Throwable error) throws IOException {
        log.error("websocket发生错误");
        error.printStackTrace();
        if (session.isOpen()) {
            session.close();
        }
    }

    */
/**
     * 关闭网页时调用
     *
     * @param session
     * @throws IOException
     *//*

    @OnClose
    public void close(Session session) {
        try {
            Iterator<Session> iterator = sessions.iterator();
            while (iterator.hasNext()) {
                if (iterator.next().equals(session)) {
                    iterator.remove();
                }
            }
        } catch (Exception e) {
            log.error("websocket连接关闭异常");
            e.printStackTrace();
        }

    }

    */
/**
     * a集合参照b集合顺序进行排序
     * @param a
     * @param b
     *//*

    public void sortCarcode(List<JianHuYiListView> a, List<String> b) {
        Collections.sort(a, ((o1, o2) -> {
            int io1 = b.indexOf(o1.getCar_code());
            int io2 = b.indexOf(o2.getCar_code());
            if (io1 != -1) {
                io1 = a.size() - io1;
            }
            if (io2 != -1) {
                io2 = a.size() - io2;
            }
            return io2 - io1;
        }));
    }
}
*/
